// 导入axios实例
import httpRequest from '../request/index';
// sha256加密
import sha256 from 'crypto-js/sha256';


// 获取动漫全部年份
export function getYears() {
    return httpRequest({
        url: `/animation/years?time=${Math.round(new Date().getTime() / 1000)}`,
        method: 'get',
    });
}


// 获取全部动漫（年份分组）
export function getAll() {
    return httpRequest({
        url: `/animation/all?time=${Math.round(new Date().getTime() / 1000)}`,
        method: 'get',
    });
}

// 获取全部动漫（不分组）
export function getAllAnimation(isAdmin = false) {
    if (isAdmin) {
        var header = { 'X-Admin-Req': 'Admin' }
    } else {
        var header = {}
    }
    return httpRequest({
        url: `/animation/listall?time=${Math.round(new Date().getTime() / 1000)}`,
        method: 'get',
        headers: header,
    });
}


// 分页获取动漫列表数据
export function getList(pageSize, pageNum) {
    return httpRequest({
        url: `/animation?pageSize=${pageSize}&pageNum=${pageNum}&time=${Math.round(new Date().getTime() / 1000)}`,
        method: 'get',
    });
}


// 获取动漫全部集数链接
export function getAnimationLinks(id, isAdmin = false) {
    if (isAdmin) {
        var header = { 'X-Admin-Req': 'Admin' }
    } else {
        var header = {}
    }
    return httpRequest({
        url: `/animation/vlist?aid=${id}&time=${Math.round(new Date().getTime() / 1000)}`,
        method: 'get',
        headers: header,
    });
}


// 添加动漫
export function addAnimation(animation) {
    animation['time'] = Math.round(new Date().getTime() / 1000);
    let params = `${animation.name}${animation.url}${animation.category}${animation.year}XpFaEDYkQH`;
    animation['sign'] = sha256(params).toString().substring(0, 15);
    return httpRequest({
        baseURL: '/admin',
        url: `/animation/add`,
        method: 'post',
        data: animation,
        headers: { Authorization: `Bearer ${window.sessionStorage.getItem('token')}` }
    });
}


// 编辑动漫
export function editAnimation(id, animation) {
    animation['time'] = Math.round(new Date().getTime() / 1000);
    let params = `${animation.name}${animation.url}${animation.category}${animation.year}XpFaEDYkQH`;
    animation['sign'] = sha256(params).toString().substring(0, 15);
    return httpRequest({
        baseURL: '/admin',
        url: `/animation/edit/${id}`,
        method: 'put',
        data: animation,
        headers: { Authorization: `Bearer ${window.sessionStorage.getItem('token')}` }
    });
}


// 删除动漫
export function deleteAnimation(id) {
    let sign = sha256(id + 'XpFaEDYkQH').toString().substring(0, 15);
    return httpRequest({
        baseURL: '/admin',
        url: `/animation/${id}?time=${Math.round(new Date().getTime() / 1000)}&sign=${sign}`,
        method: 'delete',
        headers: { Authorization: `Bearer ${window.sessionStorage.getItem('token')}` }
    });
}


// 写评语
export function doAnimationComment(id, comment) {
    comment['time'] = Math.round(new Date().getTime() / 1000);
    return httpRequest({
        baseURL: '/admin',
        url: `/animation/comment/${id}`,
        method: 'post',
        data: comment,
        headers: { Authorization: `Bearer ${window.sessionStorage.getItem('token')}` }
    });
}
